package com.gxuwz.Market.business.action.web;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceBranch;
import com.gxuwz.Market.business.entity.ServiceClass;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.entity.ServiceStudio;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.Market.business.service.IServiceBranchService;
import com.gxuwz.Market.business.service.IServiceMajorService;
import com.gxuwz.Market.business.service.ISysUserService;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ServiceBranchAction extends BaseAction implements Preparable, ModelDriven<ServiceBranch> {

	protected static final String LIST_JSP = "/WEB-INF/page/web/ServiceBranch_list.jsp";  
	protected static final String ADD_JSP = "/WEB-INF/page/web/ServiceBranch_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/web/ServiceBranch_edit.jsp";
	
	@Autowired
	private IServiceBranchService serviceBranchService;
	private List<ServiceBranch> branchList;
	private List<ServiceBranch> academyList=new ArrayList<ServiceBranch>();
	private List<ServiceBranch> majorList =new ArrayList<ServiceBranch>();
	private List<ServiceBranch> classList=new ArrayList<ServiceBranch>() ;
	private List<ServiceBranch> studioList =new ArrayList<ServiceBranch>();
	private List<ServiceBranch> userList=new ArrayList<ServiceBranch>();
	private ServiceBranch serviceBranch;
	private Result<ServiceBranch> pageResult;
	
	private List<SysUser> sysUserList;
	private List<ServiceAcademy>academyList1;//学院列表
	private List<ServiceMajor>SerMajor;//专业列表
	

	private List<ServiceClass>SerCalss;//班级列表
	private List<ServiceStudio>SerStusdio;//研究室列表
	private ServiceBranch b;
	@Autowired
	private ISysUserService sysUserSeverice;
	@Autowired
	private IServiceMajorService serviceMajorService;
	
	
	@Override
	public void prepare() throws Exception {
		if(null == serviceBranch){
			serviceBranch = new ServiceBranch();
		}
	}

	@Override
	public ServiceBranch getModel() {
		return serviceBranch;
	}
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	
	/**
	 * 打开联系人列表页面
	 * @return
	 */
	public String openList(){
		
		String userid = (String)getRequest().getSession().getAttribute("userId");
		branchList = serviceBranchService.findByProperty("parentId", userid);
	    if(branchList != null && branchList.size()>0){
			for(ServiceBranch branch:branchList){
				switch (branch.getBranchType()) {
				case 0:
					//个人id
					//branch.getParentId();
					userList.add(branch);
					//System.out.print("这是个人"+userList.get(0).getParentId());
					break;
				case 1:
					//班级
					classList.add(branch);
					//System.out.print("这是班级"+classList.get(0).getParentId());
					break;
				case 2:
					//
					majorList.add(branch);
					//System.out.print("这是专业"+branch.getParentId());
					break;
				case 3:
					//branch.getParentId();
					academyList.add(branch);
					//System.out.print("这是学院"+academyList.get(0).getSonId());
					break;
				case 4:
					studioList.add(branch);
					//System.out.print("这是科室"+branch.getParentId());
					break;

				default:
					break;
				}
			}
	    }
		setForwardView(LIST_JSP);
		return SUCCESS;
	}

	
	/**
	 * 删除
	 * @return
	 */
	public String delete() {
		serviceBranchService.delete(serviceBranch.getId());
		serviceBranch.setId(null);
		return openList();
	}

	/**
	 * 打开修改页面
	 * @return
	 */
	public String openEdit(){
		serviceBranch = serviceBranchService.findById(serviceBranch.getId());
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}

	public String openAdd() {
		
		sysUserList = sysUserSeverice.findAll();//个人
		SerCalss = sysUserSeverice.findAllClass();//班级
		SerMajor = serviceMajorService.findAll();//专业
		academyList1 = sysUserSeverice.findAllAcademy();//学院
		SerStusdio = sysUserSeverice.findStudio();//科室
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	public String add() {		
		String userid = (String)getRequest().getSession().getAttribute("userId");
		serviceBranch.setParentId(userid);
		Integer branchType = serviceBranch.getBranchType();
		String sonid= serviceBranch.getSonId();
		String sonkey= serviceBranch.getSonKey();
		
		List<ServiceBranch> branch = serviceBranchService.findByProperty2("parentId", userid, "sonId", sonid);
		if(branch == null || branch.size() == 0){
			serviceBranch.setBranchType(branchType);
			serviceBranch.setSonId(sonid);
			serviceBranch.setSonKey(sonkey);
			serviceBranchService.add(serviceBranch);
		}
		

		serviceBranch =new ServiceBranch();
		return openList();
	}
	
	
	public void usertoJson(){
		//System.out.print("haha");
		//System.out.print(serviceBranch.getSonKey());
		sysUserList = sysUserSeverice.findUserByClassName(serviceBranch.getSonKey());
		printWriter = getPrintWriter();
		Gson gson0 = new Gson();
		String data7 = gson0.toJson(sysUserList);
		printWriter.write(data7);
		printWriter.flush();
		printWriter.close();
		
	}
	
	
	public void classtoJson(){
		SerCalss = sysUserSeverice.findByMajorId(serviceBranch.getSonId());
		printWriter = getPrintWriter();
		Gson gson0 = new Gson();
		String data6 = gson0.toJson(SerCalss);
		printWriter.write(data6);
		printWriter.flush();
		printWriter.close();
	}
	
	public void majortoJson(){
		  SerMajor = sysUserSeverice.findByAca_Id(serviceBranch.getSonId());
		  printWriter = getPrintWriter();
			Gson gson0 = new Gson();
			String data5 = gson0.toJson(SerMajor);
			printWriter.write(data5);
			printWriter.flush();
			printWriter.close();
		  
		
	}
	
	
	public String edit(){
		if(null != serviceBranch){
			serviceBranchService.update(serviceBranch);
		}
		serviceBranch =new ServiceBranch();
		return openList();
	}
	public String listtoJson(){
		//b.setBranchType(getParameters(""));
		System.out.print(b.getBranchType());
		
		//String id =getParameters("branchType");
		
		switch (b.getBranchType()){
		case 0:
			sysUserList = sysUserSeverice.findAll();//个人
			printWriter = getPrintWriter();
			Gson gson0 = new Gson();
			String data0 = gson0.toJson(sysUserList);
			printWriter.write(data0);
			printWriter.flush();
			printWriter.close();
			break;
		case 1:
			
			SerCalss = sysUserSeverice.findAllClass();//班级
			printWriter = getPrintWriter();
			Gson gson1 = new Gson();
			String data1 = gson1.toJson(SerCalss);
			printWriter.write(data1);
			printWriter.flush();
			printWriter.close();
			break;
		case 2:
			SerMajor = serviceMajorService.findAll();//专业
			printWriter = getPrintWriter();
			Gson gson2 = new Gson();
			String data2 = gson2.toJson(SerMajor);
			printWriter.write(data2);
			printWriter.flush();
			printWriter.close();
			//System.out.print("拿到数据了");
			break;
		case 3:
			academyList1 = sysUserSeverice.findAllAcademy();//学院
			printWriter = getPrintWriter();
			Gson gson3 = new Gson();
			String data3 = gson3.toJson(academyList1);
			printWriter.write(data3);
			printWriter.flush();
			printWriter.close();
			break;
		case 4:
			SerStusdio = sysUserSeverice.findStudio();//科室
			printWriter = getPrintWriter();
			Gson gson4 = new Gson();
			String data4 = gson4.toJson(SerStusdio);
			printWriter.write(data4);
			printWriter.flush();
			printWriter.close();
			break;
		}
		return null;
	}
	public ServiceBranch getServiceBranch() {
		return serviceBranch;
	}

	public void setServiceBranch(ServiceBranch serviceBranch) {
		this.serviceBranch = serviceBranch;
	}

	public Result<ServiceBranch> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<ServiceBranch> pageResult) {
		this.pageResult = pageResult;
	}

	public List<ServiceBranch> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<ServiceBranch> branchList) {
		this.branchList = branchList;
	}

	public List<ServiceBranch> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<ServiceBranch> academyList) {
		this.academyList = academyList;
	}

	public List<ServiceBranch> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<ServiceBranch> majorList) {
		this.majorList = majorList;
	}

	public List<ServiceBranch> getClassList() {
		return classList;
	}

	public void setClassList(List<ServiceBranch> classList) {
		this.classList = classList;
	}

	public List<ServiceBranch> getStudioList() {
		return studioList;
	}

	public void setStudioList(List<ServiceBranch> studioList) {
		this.studioList = studioList;
	}

	public List<ServiceBranch> getUserList() {
		return userList;
	}

	public void setUserList(List<ServiceBranch> userList) {
		this.userList = userList;
	}

	public List<ServiceAcademy> getAcademyList1() {
		return academyList1;
	}

	public void setAcademyList1(List<ServiceAcademy> academyList1) {
		this.academyList1 = academyList1;
	}

	public List<ServiceMajor> getSerMajor() {
		return SerMajor;
	}

	public void setSerMajor(List<ServiceMajor> serMajor) {
		SerMajor = serMajor;
	}

	public List<ServiceClass> getSerCalss() {
		return SerCalss;
	}

	public void setSerCalss(List<ServiceClass> serCalss) {
		SerCalss = serCalss;
	}

	public List<ServiceStudio> getSerStusdio() {
		return SerStusdio;
	}

	public void setSerStusdio(List<ServiceStudio> serStusdio) {
		SerStusdio = serStusdio;
	}

	public ISysUserService getSysUserSeverice() {
		return sysUserSeverice;
	}

	public void setSysUserSeverice(ISysUserService sysUserSeverice) {
		this.sysUserSeverice = sysUserSeverice;
	}

	public IServiceMajorService getServiceMajorService() {
		return serviceMajorService;
	}

	public void setServiceMajorService(IServiceMajorService serviceMajorService) {
		this.serviceMajorService = serviceMajorService;
	}

	public List<SysUser> getSysUserList() {
		return sysUserList;
	}

	public void setSysUserList(List<SysUser> sysUserList) {
		this.sysUserList = sysUserList;
	}

	public ServiceBranch getB() {
		return b;
	}

	public void setB(ServiceBranch b) {
		this.b = b;
	}


	
	
	
}
